home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume22 / gawk2.11 / part01 next >
Encoding:
Internet Message Format  |  1990-06-07  |  51.1 KB

  1. Subject:  v22i087:  GNU AWK, version 2.11, Part01/16
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 1e0bfe39 842e72d0 027e4998 673f1534
  5.  
  6. Submitted-by: "Arnold D. Robbins" <arnold@unix.cc.emory.edu>
  7. Posting-number: Volume 22, Issue 87
  8. Archive-name: gawk2.11/part01
  9.  
  10. [  This is the GNU version of AWK.  It is compatible with NAWK, the
  11.    "new awk" described in the AWK book, and shipped with SysVr4.
  12.  
  13.    A couple of files were split into pieces for posting; they are
  14.    noted in the MANIFEST.  --r$  ]
  15.  
  16. I think I mentioned that we're dropping the 'beta' label on gawk with
  17. the c.s.u. release.  But I wanted to mention that gawk has been endorsed
  18. by Rick Adams of UUNET.  He replaced /bin/awk with gawk for all his work
  19. on UUNET...
  20.  
  21. Arnold
  22.  
  23. #! /bin/sh
  24. # This is a shell archive.  Remove anything before this line, then feed it
  25. # into a shell via "sh file" or similar.  To overwrite existing files,
  26. # type "sh file -c".
  27. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  28. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  29. # Contents:  MANIFEST ./missing.d ./missing.d/getopt.c ./pc.d ./support
  30. #   ./support/texindex.c
  31. # Wrapped by rsalz@litchi.bbn.com on Wed Jun  6 12:24:45 1990
  32. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  33. echo If this archive is complete, you will see the following message:
  34. echo '          "shar: End of archive 1 (of 16)."'
  35. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  36.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  37. else
  38.   echo shar: Extracting \"'MANIFEST'\" \(1881 characters\)
  39.   sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  40. X   File Name        Archive #    Description
  41. X----------------------------------------------------------
  42. XMANIFEST                   1    This shipping list
  43. X./CHANGES                 15    
  44. X./COPYING                 14    
  45. X./FUTURES                  9    
  46. X./Makefile                15    
  47. X./PROBLEMS                 9    
  48. X./README                  16    
  49. X./alloca.c                16    
  50. X./alloca.s                15    
  51. X./array.c                 16    
  52. X./awk.h                   13    
  53. X./awk.y                   10    
  54. X./builtin.c               12    
  55. X./debug.c                 14    
  56. X./eval.c                  12    
  57. X./field.c                 15    
  58. X./gawk.1                  11    
  59. X./gawk.texinfo.01          2    (part 1)
  60. X./gawk.texinfo.02          5    (part 2)
  61. X./gawk.texinfo.03          6    (part 3)
  62. X./gawk.texinfo.04          3    (part 4)
  63. X./gawk.texinfo.05          4    (part 5)
  64. X./gawk.texinfo.06          7    (part 6)
  65. X./gawk.texinfo.07         15    (part 7)
  66. X./io.c                    11    
  67. X./main.c                  14    
  68. X./missing.c                8    
  69. X./missing.d                1    
  70. X./missing.d/dup2.c         9    
  71. X./missing.d/gcvt.c        12    
  72. X./missing.d/getopt.c       1    
  73. X./missing.d/memcmp.c       9    
  74. X./missing.d/memcpy.c      11    
  75. X./missing.d/memset.c      10    
  76. X./missing.d/random.c      10    
  77. X./missing.d/strcase.c     16    
  78. X./missing.d/strchr.c       6    
  79. X./missing.d/strerror.c     7    
  80. X./missing.d/strtod.c       3    
  81. X./missing.d/tmpnam.c       7    
  82. X./missing.d/vprintf.c      8    
  83. X./msg.c                    2    
  84. X./node.c                  15    
  85. X./patchlevel.h             4    
  86. X./pc.d                     1    
  87. X./pc.d/Makefile.pc        13    
  88. X./pc.d/popen.c             4    
  89. X./pc.d/popen.h            10    
  90. X./regex.c.01               8    (part 1)
  91. X./regex.c.02               5    (part 2)
  92. X./regex.h                 14    
  93. X./support                  1    
  94. X./support/texindex.c       1    
  95. X./support/texinfo.tex.01   9    (part 1)
  96. X./support/texinfo.tex.02  13    (part 2)
  97. X./version.sh               6    
  98. END_OF_FILE
  99.   if test 1881 -ne `wc -c <'MANIFEST'`; then
  100.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  101.   fi
  102.   # end of 'MANIFEST'
  103. fi
  104. if test ! -d './missing.d' ; then
  105.     echo shar: Creating directory \"'./missing.d'\"
  106.     mkdir './missing.d'
  107. fi
  108. if test -f './missing.d/getopt.c' -a "${1}" != "-c" ; then 
  109.   echo shar: Will not clobber existing file \"'./missing.d/getopt.c'\"
  110. else
  111.   echo shar: Extracting \"'./missing.d/getopt.c'\" \(2434 characters\)
  112.   sed "s/^X//" >'./missing.d/getopt.c' <<'END_OF_FILE'
  113. X/*
  114. X**    @(#)getopt.c    2.5 (smail) 9/15/87
  115. X*/
  116. X
  117. X/*
  118. X * Here's something you've all been waiting for:  the AT&T public domain
  119. X * source for getopt(3).  It is the code which was given out at the 1985
  120. X * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  121. X * directly from AT&T.  The people there assure me that it is indeed
  122. X * in the public domain.
  123. X * 
  124. X * There is no manual page.  That is because the one they gave out at
  125. X * UNIFORUM was slightly different from the current System V Release 2
  126. X * manual page.  The difference apparently involved a note about the
  127. X * famous rules 5 and 6, recommending using white space between an option
  128. X * and its first argument, and not grouping options that have arguments.
  129. X * Getopt itself is currently lenient about both of these things White
  130. X * space is allowed, but not mandatory, and the last option in a group can
  131. X * have an argument.  That particular version of the man page evidently
  132. X * has no official existence, and my source at AT&T did not send a copy.
  133. X * The current SVR2 man page reflects the actual behavor of this getopt.
  134. X * However, I am not about to post a copy of anything licensed by AT&T.
  135. X */
  136. X
  137. X#if defined(MSDOS) || defined(USG)
  138. X#define index strchr
  139. X#endif
  140. X
  141. X/*LINTLIBRARY*/
  142. X#define NULL    0
  143. X#define EOF    (-1)
  144. X#define ERR(s, c)    if(opterr){\
  145. X    extern int write();\
  146. X    char errbuf[2];\
  147. X    errbuf[0] = c; errbuf[1] = '\n';\
  148. X    (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  149. X    (void) write(2, s, (unsigned)strlen(s));\
  150. X    (void) write(2, errbuf, 2);}
  151. X
  152. Xextern char *index();
  153. X
  154. Xint    opterr = 1;
  155. Xint    optind = 1;
  156. Xint    optopt;
  157. Xchar    *optarg;
  158. X
  159. Xint
  160. Xgetopt(argc, argv, opts)
  161. Xint    argc;
  162. Xchar    **argv, *opts;
  163. X{
  164. X    static int sp = 1;
  165. X    register int c;
  166. X    register char *cp;
  167. X
  168. X    if(sp == 1)
  169. X        if(optind >= argc ||
  170. X           argv[optind][0] != '-' || argv[optind][1] == '\0')
  171. X            return(EOF);
  172. X        else if(strcmp(argv[optind], "--") == NULL) {
  173. X            optind++;
  174. X            return(EOF);
  175. X        }
  176. X    optopt = c = argv[optind][sp];
  177. X    if(c == ':' || (cp=index(opts, c)) == NULL) {
  178. X        ERR(": illegal option -- ", c);
  179. X        if(argv[optind][++sp] == '\0') {
  180. X            optind++;
  181. X            sp = 1;
  182. X        }
  183. X        return('?');
  184. X    }
  185. X    if(*++cp == ':') {
  186. X        if(argv[optind][sp+1] != '\0')
  187. X            optarg = &argv[optind++][sp+1];
  188. X        else if(++optind >= argc) {
  189. X            ERR(": option requires an argument -- ", c);
  190. X            sp = 1;
  191. X            return('?');
  192. X        } else
  193. X            optarg = argv[optind++];
  194. X        sp = 1;
  195. X    } else {
  196. X        if(argv[optind][++sp] == '\0') {
  197. X            sp = 1;
  198. X            optind++;
  199. X        }
  200. X        optarg = NULL;
  201. X    }
  202. X    return(c);
  203. X}
  204. END_OF_FILE
  205.   if test 2434 -ne `wc -c <'./missing.d/getopt.c'`; then
  206.     echo shar: \"'./missing.d/getopt.c'\" unpacked with wrong size!
  207.   fi
  208.   # end of './missing.d/getopt.c'
  209. fi
  210. if test ! -d './pc.d' ; then
  211.     echo shar: Creating directory \"'./pc.d'\"
  212.     mkdir './pc.d'
  213. fi
  214. if test ! -d './support' ; then
  215.     echo shar: Creating directory \"'./support'\"
  216.     mkdir './support'
  217. fi
  218. if test -f './support/texindex.c' -a "${1}" != "-c" ; then 
  219.   echo shar: Will not clobber existing file \"'./support/texindex.c'\"
  220. else
  221.   echo shar: Extracting \"'./support/texindex.c'\" \(42774 characters\)
  222.   sed "s/^X//" >'./support/texindex.c' <<'END_OF_FILE'
  223. X/* Prepare Tex index dribble output into an actual index.
  224. X   Copyright (C) 1987 Free Software Foundation, Inc.
  225. X
  226. X               NO WARRANTY
  227. X
  228. X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  229. XNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  230. XWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  231. XRICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  232. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  233. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  234. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  235. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  236. XDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  237. XCORRECTION.
  238. X
  239. X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  240. XSTALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  241. XWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  242. XLIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  243. XOTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  244. XUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  245. XDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  246. XA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  247. XPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  248. XDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  249. X
  250. X        GENERAL PUBLIC LICENSE TO COPY
  251. X
  252. X  1. You may copy and distribute verbatim copies of this source file
  253. Xas you receive it, in any medium, provided that you conspicuously
  254. Xand appropriately publish on each copy a valid copyright notice
  255. X"Copyright (C) 1987 Free Software Foundation, Inc.", and include
  256. Xfollowing the copyright notice a verbatim copy of the above disclaimer
  257. Xof warranty and of this License.
  258. X
  259. X  2. You may modify your copy or copies of this source file or
  260. Xany portion of it, and copy and distribute such modifications under
  261. Xthe terms of Paragraph 1 above, provided that you also do the following:
  262. X
  263. X    a) cause the modified files to carry prominent notices stating
  264. X    that you changed the files and the date of any change; and
  265. X
  266. X    b) cause the whole of any work that you distribute or publish,
  267. X    that in whole or in part contains or is a derivative of this
  268. X    program or any part thereof, to be licensed at no charge to all
  269. X    third parties on terms identical to those contained in this
  270. X    License Agreement (except that you may choose to grant more extensive
  271. X    warranty protection to some or all third parties, at your option).
  272. X
  273. X    c) You may charge a distribution fee for the physical act of
  274. X    transferring a copy, and you may at your option offer warranty
  275. X    protection in exchange for a fee.
  276. X
  277. XMere aggregation of another unrelated program with this program (or its
  278. Xderivative) on a volume of a storage or distribution medium does not bring
  279. Xthe other program under the scope of these terms.
  280. X
  281. X  3. You may copy and distribute this program (or a portion or derivative
  282. Xof it, under Paragraph 2) in object code or executable form under the terms
  283. Xof Paragraphs 1 and 2 above provided that you also do one of the following:
  284. X
  285. X    a) accompany it with the complete corresponding machine-readable
  286. X    source code, which must be distributed under the terms of
  287. X    Paragraphs 1 and 2 above; or,
  288. X
  289. X    b) accompany it with a written offer, valid for at least three
  290. X    years, to give any third party free (except for a nominal
  291. X    shipping charge) a complete machine-readable copy of the
  292. X    corresponding source code, to be distributed under the terms of
  293. X    Paragraphs 1 and 2 above; or,
  294. X
  295. X    c) accompany it with the information you received as to where the
  296. X    corresponding source code may be obtained.  (This alternative is
  297. X    allowed only for noncommercial distribution and only if you
  298. X    received the program in object code or executable form alone.)
  299. X
  300. XFor an executable file, complete source code means all the source code for
  301. Xall modules it contains; but, as a special exception, it need not include
  302. Xsource code for modules which are standard libraries that accompany the
  303. Xoperating system on which the executable file runs.
  304. X
  305. X  4. You may not copy, sublicense, distribute or transfer this program
  306. Xexcept as expressly provided under this License Agreement.  Any attempt
  307. Xotherwise to copy, sublicense, distribute or transfer this program is void and
  308. Xyour rights to use the program under this License agreement shall be
  309. Xautomatically terminated.  However, parties who have received computer
  310. Xsoftware programs from you with this License Agreement will not have
  311. Xtheir licenses terminated so long as such parties remain in full compliance.
  312. X
  313. X  5. If you wish to incorporate parts of this program into other free
  314. Xprograms whose distribution conditions are different, write to the Free
  315. XSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  316. Xworked out a simple rule that can be stated here, but we will often permit
  317. Xthis.  We will be guided by the two goals of preserving the free status of
  318. Xall derivatives of our free software and of promoting the sharing and reuse of
  319. Xsoftware.
  320. X
  321. X In other words, you are welcome to use, share and improve this program.
  322. X You are forbidden to forbid anyone else to use, share and improve
  323. X what you give them.   Help stamp out software-hoarding!  */
  324. X
  325. X
  326. X#include <stdio.h>
  327. X#include <ctype.h>
  328. X
  329. X#ifdef VMS
  330. X#include <file.h>
  331. X
  332. X#define EXIT_SUCCESS ((1 << 28) | 1)
  333. X#define EXIT_FATAL ((1 << 28) | 4)
  334. X#define unlink delete
  335. X#define tell(fd) lseek(fd, 0L, 1)
  336. X#else
  337. X#include <sys/file.h>
  338. X
  339. X#define EXIT_SUCCESS 0
  340. X#define EXIT_FATAL 1
  341. X#endif
  342. X
  343. X
  344. X#ifndef L_XTND
  345. X#define L_XTND 2
  346. X#endif
  347. X
  348. X/* When sorting in core, this structure describes one line
  349. X and the position and length of its first keyfield.  */
  350. X
  351. Xstruct lineinfo
  352. X  {
  353. X    char *text;        /* The actual text of the line */
  354. X    union
  355. X      {            /* The start of the key (for textual comparison) */
  356. X    char *text;
  357. X    long number;    /* or the numeric value (for numeric comparison) */
  358. X      } key;
  359. X    long keylen;    /* Length of key field */
  360. X  };
  361. X
  362. X/* This structure describes a field to use as a sort key */
  363. X
  364. Xstruct keyfield
  365. X  {
  366. X    int startwords;        /* # words to skip  */
  367. X    int startchars;        /*  and # additional chars to skip, to start of field */
  368. X    int endwords;        /* similar, from beg (or end) of line, to find end of field */
  369. X    int endchars;
  370. X    char ignore_blanks;        /* Ignore spaces and tabs within the field */
  371. X    char fold_case;        /* Convert upper case to lower before comparing */
  372. X    char reverse;        /* Compare in reverse order */
  373. X    char numeric;        /* Parse text as an integer and compare the integers */
  374. X    char positional;        /* Sort according to position within the file */
  375. X    char braced;        /* Count balanced-braced groupings as fields */
  376. X  };
  377. X
  378. X/* Vector of keyfields to use */
  379. X
  380. Xstruct keyfield keyfields[3];
  381. X
  382. X/* Number of keyfields stored in that vector.  */
  383. X
  384. Xint num_keyfields = 3;
  385. X
  386. X/* Vector of input file names, terminated with a zero (null pointer) */
  387. X
  388. Xchar **infiles;
  389. X
  390. X/* Vector of corresponding output file names, or zero meaning default it */
  391. X
  392. Xchar **outfiles;
  393. X
  394. X/* Length of `infiles' */
  395. X
  396. Xint num_infiles;
  397. X
  398. X/* Pointer to the array of pointers to lines being sorted */
  399. X
  400. Xchar **linearray;
  401. X
  402. X/* The allocated length of `linearray'. */
  403. X
  404. Xlong lines;
  405. X
  406. X/* Directory to use for temporary files.  On Unix, it ends with a slash.  */
  407. X
  408. Xchar *tempdir;
  409. X
  410. X/* Start of filename to use for temporary files.  */
  411. X
  412. Xchar *tempbase;
  413. X
  414. X/* Number of last temporary file.  */
  415. X
  416. Xint tempcount;
  417. X
  418. X/* Number of last temporary file already deleted.
  419. X Temporary files are deleted by `flush_tempfiles' in order of creation.  */
  420. X
  421. Xint last_deleted_tempcount;
  422. X
  423. X/* During in-core sort, this points to the base of the data block
  424. X which contains all the lines of data.  */
  425. X
  426. Xchar *text_base;
  427. X
  428. X/* Additional command switches */
  429. X
  430. Xint keep_tempfiles;    /* Nonzero means do not delete tempfiles -- for debugging */
  431. X
  432. X/* Forward declarations of functions in this file */
  433. X
  434. Xvoid decode_command ();
  435. Xvoid sort_in_core ();
  436. Xvoid sort_offline ();
  437. Xchar **parsefile ();
  438. Xchar *find_field ();
  439. Xchar *find_pos ();
  440. Xlong find_value ();
  441. Xchar *find_braced_pos ();
  442. Xchar *find_braced_end ();
  443. Xvoid writelines ();
  444. Xint compare_full ();
  445. Xlong readline ();
  446. Xint merge_files ();
  447. Xint merge_direct ();
  448. Xchar *concat ();
  449. Xchar *maketempname ();
  450. Xvoid flush_tempfiles ();
  451. Xchar *tempcopy ();
  452. X
  453. Xextern char *mktemp ();
  454. X
  455. X#define MAX_IN_CORE_SORT 500000
  456. X
  457. Xint
  458. Xmain (argc, argv)
  459. X     int argc;
  460. X     char **argv;
  461. X{
  462. X  int i;
  463. X
  464. X  tempcount = 0;
  465. X  last_deleted_tempcount = 0;
  466. X
  467. X  /* Describe the kind of sorting to do. */
  468. X  /* The first keyfield uses the first braced field and folds case */
  469. X  keyfields[0].braced = 1;
  470. X  keyfields[0].fold_case = 1;
  471. X  keyfields[0].endwords = -1;
  472. X  keyfields[0].endchars = -1;
  473. X  /* The second keyfield uses the second braced field, numerically */
  474. X  keyfields[1].braced = 1;
  475. X  keyfields[1].numeric = 1;
  476. X  keyfields[1].startwords = 1;
  477. X  keyfields[1].endwords = -1;
  478. X  keyfields[1].endchars = -1;
  479. X  /* The third keyfield (which is ignored while discarding duplicates)
  480. X     compares the whole line */
  481. X  keyfields[2].endwords = -1;
  482. X  keyfields[2].endchars = -1;
  483. X
  484. X  decode_command (argc, argv);
  485. X
  486. X  tempbase = mktemp (concat ("txiXXXXXX", "", ""));
  487. X
  488. X  /* Process input files completely, one by one.  */
  489. X
  490. X  for (i = 0; i < num_infiles; i++)
  491. X    {
  492. X      int desc;
  493. X      long ptr;
  494. X      char *outfile;
  495. X      char *p;
  496. X
  497. X      desc = open (infiles[i], 0, 0);
  498. X      if (desc < 0) pfatal_with_name (infiles[i]);
  499. X      lseek (desc, 0, L_XTND);
  500. X      ptr = tell (desc);
  501. X      close (desc);
  502. X
  503. X      outfile = outfiles[i];
  504. X      if (!outfile)
  505. X    {
  506. X      outfile = concat (infiles[i], "s", "");
  507. X    }
  508. X
  509. X      if (ptr < MAX_IN_CORE_SORT)
  510. X        /* Sort a small amount of data */
  511. X        sort_in_core (infiles[i], ptr, outfile);
  512. X      else
  513. X        sort_offline (infiles[i], ptr, outfile);
  514. X    }
  515. X
  516. X  flush_tempfiles (tempcount);
  517. X  exit (EXIT_SUCCESS);
  518. X}
  519. X
  520. X/* This page decodes the command line arguments to set the parameter variables
  521. X and set up the vector of keyfields and the vector of input files */
  522. X
  523. Xvoid
  524. Xdecode_command (argc, argv)
  525. X     int argc;
  526. X     char **argv;
  527. X{
  528. X  int i;
  529. X  char **ip;
  530. X  char **op;
  531. X
  532. X  /* Store default values into parameter variables */
  533. X
  534. X#ifdef VMS
  535. X  tempdir = "sys$scratch:";
  536. X#else
  537. X  tempdir = "/tmp/";
  538. X#endif
  539. X
  540. X  keep_tempfiles = 0;
  541. X
  542. X  /* Allocate argc input files, which must be enough.  */
  543. X
  544. X  infiles = (char **) xmalloc (argc * sizeof (char *));
  545. X  outfiles = (char **) xmalloc (argc * sizeof (char *));
  546. X  ip = infiles;
  547. X  op = outfiles;
  548. X
  549. X  /* First find all switches that control the default kind-of-sort */
  550. X
  551. X  for (i = 1; i < argc; i++)
  552. X    {
  553. X      int tem = classify_arg (argv[i]);
  554. X      char c;
  555. X      char *p;
  556. X
  557. X      if (tem <= 0)
  558. X    {
  559. X      *ip++ = argv[i];
  560. X      *op++ = 0;
  561. X      continue;
  562. X    }
  563. X      if (tem > 1)
  564. X    {
  565. X      if (i + 1 == argc)
  566. X        fatal ("switch %s given with no argument following it", argv[i]);
  567. X      else if (!strcmp (argv[i], "-T"))
  568. X        tempdir = argv[i + 1];
  569. X      else if (!strcmp (argv[i], "-o"))
  570. X        *(op - 1) = argv[i + 1];
  571. X      i += tem - 1;
  572. X      continue;
  573. X    }
  574. X
  575. X      p = &argv[i][1];
  576. X      while (c = *p++)
  577. X    switch (c)
  578. X      {
  579. X      case 'k':
  580. X        keep_tempfiles = 1;
  581. X        break;
  582. X
  583. X      default:
  584. X        fatal ("invalid command switch %c", c);
  585. X      }
  586. X    switchdone: ;
  587. X    }
  588. X
  589. X  /* Record number of keyfields, terminate list of filenames */
  590. X
  591. X  num_infiles = ip - infiles;
  592. X  *ip = 0;
  593. X}
  594. X
  595. X/* Return 0 for an argument that is not a switch;
  596. X for a switch, return 1 plus the number of following arguments that the switch swallows.
  597. X*/
  598. X
  599. Xint
  600. Xclassify_arg (arg)
  601. X     char *arg;
  602. X{
  603. X  if (!strcmp (arg, "-T") || !strcmp (arg, "-o"))
  604. X    return 2;
  605. X  if (arg[0] == '-')
  606. X    return 1;
  607. X  return 0;
  608. X}
  609. X
  610. X/* Create a name for a temporary file */
  611. X
  612. Xchar *
  613. Xmaketempname (count)
  614. X     int count;
  615. X{
  616. X  char tempsuffix[10];
  617. X  sprintf (tempsuffix, "%d", count);
  618. X  return concat (tempdir, tempbase, tempsuffix);
  619. X}
  620. X
  621. X/* Delete all temporary files up to the specified count */
  622. X
  623. Xvoid
  624. Xflush_tempfiles (to_count)
  625. X     int to_count;
  626. X{
  627. X  if (keep_tempfiles) return;
  628. X  while (last_deleted_tempcount < to_count)
  629. X    unlink (maketempname (++last_deleted_tempcount));
  630. X}
  631. X
  632. X/* Copy an input file into a temporary file, and return the temporary file name */
  633. X
  634. X#define BUFSIZE 1024
  635. X
  636. Xchar *
  637. Xtempcopy (idesc)
  638. X     int idesc;
  639. X{
  640. X  char *outfile = maketempname (++tempcount);
  641. X  int odesc;
  642. X  char buffer[BUFSIZE];
  643. X
  644. X  odesc = open (outfile, O_WRONLY | O_CREAT, 0666);
  645. X
  646. X  if (odesc < 0) pfatal_with_name (outfile);
  647. X
  648. X  while (1)
  649. X    {
  650. X      int nread = read (idesc, buffer, BUFSIZE);
  651. X      write (odesc, buffer, nread);
  652. X      if (!nread) break;
  653. X    }
  654. X
  655. X  close (odesc);
  656. X
  657. X  return outfile;
  658. X}
  659. X
  660. X/* Compare two lines, provided as pointers to pointers to text,
  661. X according to the specified set of keyfields */
  662. X
  663. Xint
  664. Xcompare_full (line1, line2)
  665. X     char **line1, **line2;
  666. X{
  667. X  int i;
  668. X
  669. X  /* Compare using the first keyfield;
  670. X     if that does not distinguish the lines, try the second keyfield; and so on. */
  671. X
  672. X  for (i = 0; i < num_keyfields; i++)
  673. X    {
  674. X      long length1, length2;
  675. X      char *start1 = find_field (&keyfields[i], *line1, &length1);
  676. X      char *start2 = find_field (&keyfields[i], *line2, &length2);
  677. X      int tem = compare_field (&keyfields[i], start1, length1, *line1 - text_base,
  678. X                          start2, length2, *line2 - text_base);
  679. X      if (tem)
  680. X    {
  681. X      if (keyfields[i].reverse)
  682. X        return - tem;
  683. X          return tem;
  684. X    }
  685. X    }
  686. X
  687. X  return 0;    /* Lines match exactly */
  688. X}
  689. X
  690. X/* Compare two lines described by structures
  691. X in which the first keyfield is identified in advance.
  692. X For positional sorting, assumes that the order of the lines in core
  693. X reflects their nominal order.  */
  694. X
  695. Xint
  696. Xcompare_prepared (line1, line2)
  697. X     struct lineinfo *line1, *line2;
  698. X{
  699. X  int i;
  700. X  int tem;
  701. X  char *text1, *text2;
  702. X
  703. X  /* Compare using the first keyfield, which has been found for us already */
  704. X  if (keyfields->positional)
  705. X    {
  706. X      if (line1->text - text_base > line2->text - text_base)
  707. X    tem = 1;
  708. X      else
  709. X    tem = -1;
  710. X    }
  711. X  else if (keyfields->numeric)
  712. X    tem = line1->key.number - line2->key.number;
  713. X  else
  714. X    tem = compare_field (keyfields, line1->key.text, line1->keylen, 0, line2->key.text, line2->keylen, 0);
  715. X  if (tem)
  716. X    {
  717. X      if (keyfields->reverse)
  718. X    return - tem;
  719. X      return tem;
  720. X    }
  721. X
  722. X  text1 = line1->text;
  723. X  text2 = line2->text;
  724. X
  725. X  /* Compare using the second keyfield;
  726. X     if that does not distinguish the lines, try the third keyfield; and so on. */
  727. X
  728. X  for (i = 1; i < num_keyfields; i++)
  729. X    {
  730. X      long length1, length2;
  731. X      char *start1 = find_field (&keyfields[i], text1, &length1);
  732. X      char *start2 = find_field (&keyfields[i], text2, &length2);
  733. X      int tem = compare_field (&keyfields[i], start1, length1, text1 - text_base,
  734. X                          start2, length2, text2 - text_base);
  735. X      if (tem)
  736. X    {
  737. X      if (keyfields[i].reverse)
  738. X        return - tem;
  739. X          return tem;
  740. X    }
  741. X    }
  742. X
  743. X  return 0;    /* Lines match exactly */
  744. X}
  745. X
  746. X/* Like compare_full but more general.
  747. X You can pass any strings, and you can say how many keyfields to use.
  748. X `pos1' and `pos2' should indicate the nominal positional ordering of
  749. X the two lines in the input.  */
  750. X
  751. Xint
  752. Xcompare_general (str1, str2, pos1, pos2, use_keyfields)
  753. X     char *str1, *str2;
  754. X     long pos1, pos2;
  755. X     int use_keyfields;
  756. X{
  757. X  int i;
  758. X
  759. X  /* Compare using the first keyfield;
  760. X     if that does not distinguish the lines, try the second keyfield; and so on. */
  761. X
  762. X  for (i = 0; i < use_keyfields; i++)
  763. X    {
  764. X      long length1, length2;
  765. X      char *start1 = find_field (&keyfields[i], str1, &length1);
  766. X      char *start2 = find_field (&keyfields[i], str2, &length2);
  767. X      int tem = compare_field (&keyfields[i], start1, length1, pos1, start2, length2, pos2);
  768. X      if (tem)
  769. X    {
  770. X      if (keyfields[i].reverse)
  771. X        return - tem;
  772. X          return tem;
  773. X    }
  774. X    }
  775. X
  776. X  return 0;    /* Lines match exactly */
  777. X}
  778. X
  779. X/* Find the start and length of a field in `str' according to `keyfield'.
  780. X A pointer to the starting character is returned, and the length
  781. X is stored into the int that `lengthptr' points to.  */
  782. X
  783. Xchar *
  784. Xfind_field (keyfield, str, lengthptr)
  785. X     struct keyfield *keyfield;
  786. X     char *str;
  787. X     long *lengthptr;
  788. X{
  789. X  char *start;
  790. X  char *end;
  791. X  char *(*fun) ();
  792. X
  793. X  if (keyfield->braced) fun = find_braced_pos;
  794. X  else fun = find_pos;
  795. X
  796. X  start = ( *fun )(str, keyfield->startwords, keyfield->startchars,
  797. X           keyfield->ignore_blanks);
  798. X  if (keyfield->endwords < 0)
  799. X    {
  800. X      if (keyfield->braced)
  801. X    end = find_braced_end (start);
  802. X      else
  803. X    {
  804. X      end = start;
  805. X      while (*end && *end != '\n') end++;
  806. X    }
  807. X    }
  808. X  else
  809. X    {
  810. X      end = ( *fun )(str, keyfield->endwords, keyfield->endchars, 0);
  811. X      if (end - str < start - str) end = start;
  812. X    }
  813. X  *lengthptr = end - start;
  814. X  return start;
  815. X}
  816. X
  817. X/* Find a pointer to a specified place within `str',
  818. X skipping (from the beginning) `words' words and then `chars' chars.
  819. X If `ignore_blanks' is nonzero, we skip all blanks
  820. X after finding the specified word.  */
  821. X
  822. Xchar *
  823. Xfind_pos (str, words, chars, ignore_blanks)
  824. X     char *str;
  825. X     int words, chars;
  826. X     int ignore_blanks;
  827. X{
  828. X  int i;
  829. X  char *p = str;
  830. X
  831. X  for (i = 0; i < words; i++)
  832. X    {
  833. X      char c;
  834. X      /* Find next bunch of nonblanks and skip them. */
  835. X      while ((c = *p) == ' ' || c == '\t') p++;
  836. X      while ((c = *p) && c != '\n' && !(c == ' ' || c == '\t')) p++;
  837. X      if (!*p || *p == '\n') return p;
  838. X    }
  839. X
  840. X  while (*p == ' ' || *p == '\t') p++;
  841. X
  842. X  for (i = 0; i < chars; i++)
  843. X    {
  844. X      if (!*p  || *p == '\n') break;
  845. X      p++;
  846. X    }
  847. X  return p;
  848. X}
  849. X
  850. X/* Like find_pos but assumes that each field is surrounded by braces
  851. X and that braces within fields are balanced. */
  852. X
  853. Xchar *
  854. Xfind_braced_pos (str, words, chars, ignore_blanks)
  855. X     char *str;
  856. X     int words, chars;
  857. X     int ignore_blanks;
  858. X{
  859. X  int i;
  860. X  int bracelevel;
  861. X  char *p = str;
  862. X  char c;
  863. X
  864. X  for (i = 0; i < words; i++)
  865. X    {
  866. X      bracelevel = 1;
  867. X      while ((c = *p++) != '{' && c != '\n' && c);
  868. X      if (c != '{')
  869. X    return p - 1;
  870. X      while (bracelevel)
  871. X    {
  872. X      c = *p++;
  873. X      if (c == '{') bracelevel++;
  874. X      if (c == '}') bracelevel--;
  875. X      if (c == '\\') c = *p++;    /* \ quotes braces and \ */
  876. X      if (c == 0 || c == '\n') return p-1;
  877. X    }
  878. X    }
  879. X
  880. X  while ((c = *p++) != '{' && c != '\n' && c);
  881. X
  882. X  if (c != '{')
  883. X    return p-1;
  884. X
  885. X  if (ignore_blanks)
  886. X    while ((c = *p) == ' ' || c == '\t') p++;
  887. X  
  888. X  for (i = 0; i < chars; i++)
  889. X    {
  890. X      if (!*p  || *p == '\n') break;
  891. X      p++;
  892. X    }
  893. X  return p;
  894. X}
  895. X
  896. X/* Find the end of the balanced-brace field which starts at `str'.
  897. X  The position returned is just before the closing brace. */
  898. X
  899. Xchar *
  900. Xfind_braced_end (str)
  901. X     char *str;
  902. X{
  903. X  int bracelevel;
  904. X  char *p = str;
  905. X  char c;
  906. X
  907. X  bracelevel = 1;
  908. X  while (bracelevel)
  909. X    {
  910. X      c = *p++;
  911. X      if (c == '{') bracelevel++;
  912. X      if (c == '}') bracelevel--;
  913. X      if (c == '\\') c = *p++;
  914. X      if (c == 0 || c == '\n') return p-1;
  915. X    }
  916. X  return p - 1;
  917. X}
  918. X
  919. Xlong
  920. Xfind_value (start, length)
  921. X     char *start;
  922. X     long length;
  923. X{
  924. X  while (length != 0L) {
  925. X    if (isdigit(*start))
  926. X      return atol(start);
  927. X    length--;
  928. X    start++;
  929. X  }
  930. X  return 0l;
  931. X}
  932. X
  933. X/* Vector used to translate characters for comparison.
  934. X   This is how we make all alphanumerics follow all else,
  935. X   and ignore case in the first sorting.  */
  936. Xint char_order[256];
  937. X
  938. Xinit_char_order ()
  939. X{
  940. X  int i;
  941. X  for (i = 1; i < 256; i++)
  942. X    char_order[i] = i;
  943. X
  944. X  for (i = '0'; i <= '9'; i++)
  945. X    char_order[i] += 512;
  946. X
  947. X  for (i = 'a'; i <= 'z'; i++) {
  948. X    char_order[i] = 512 + i;
  949. X    char_order[i + 'A' - 'a'] = 512 + i;
  950. X  }
  951. X}
  952. X
  953. X/* Compare two fields (each specified as a start pointer and a character count)
  954. X according to `keyfield'.  The sign of the value reports the relation between the fields */
  955. X
  956. Xint
  957. Xcompare_field (keyfield, start1, length1, pos1, start2, length2, pos2)
  958. X     struct keyfield *keyfield;
  959. X     char *start1;
  960. X     long length1;
  961. X     long pos1;
  962. X     char *start2;
  963. X     long length2;
  964. X     long pos2;
  965. X{
  966. X  if (keyfields->positional)
  967. X    {
  968. X      if (pos1 > pos2)
  969. X    return 1;
  970. X      else
  971. X    return -1;
  972. X    }
  973. X  if (keyfield->numeric)
  974. X    {
  975. X    long value = find_value (start1, length1) - find_value (start2, length2);
  976. X      if (value > 0) return 1;
  977. X      if (value < 0) return -1;
  978. X      return 0;
  979. X    }
  980. X  else
  981. X    {
  982. X      char *p1 = start1;
  983. X      char *p2 = start2;
  984. X      char *e1 = start1 + length1;
  985. X      char *e2 = start2 + length2;
  986. X
  987. X      int fold_case = keyfield->fold_case;
  988. X
  989. X      while (1)
  990. X    {
  991. X      int c1, c2;
  992. X
  993. X      if (p1 == e1) c1 = 0;
  994. X      else c1 = *p1++;
  995. X      if (p2 == e2) c2 = 0;
  996. X      else c2 = *p2++;
  997. X
  998. X      if (char_order[c1] != char_order[c2])
  999. X        return char_order[c1] - char_order[c2];
  1000. X      if (!c1) break;
  1001. X    }
  1002. X
  1003. X      /* Strings are equal except possibly for case.  */
  1004. X      p1 = start1;
  1005. X      p2 = start2;
  1006. X      while (1)
  1007. X    {
  1008. X      int c1, c2;
  1009. X
  1010. X      if (p1 == e1) c1 = 0;
  1011. X      else c1 = *p1++;
  1012. X      if (p2 == e2) c2 = 0;
  1013. X      else c2 = *p2++;
  1014. X
  1015. X      if (c1 != c2)
  1016. X        /* Reverse sign here so upper case comes out last.  */
  1017. X        return c2 - c1;
  1018. X      if (!c1) break;
  1019. X    }
  1020. X
  1021. X      return 0;
  1022. X    }
  1023. X}
  1024. X
  1025. X/* A `struct linebuffer' is a structure which holds a line of text.
  1026. X `readline' reads a line from a stream into a linebuffer
  1027. X and works regardless of the length of the line.  */
  1028. X
  1029. Xstruct linebuffer
  1030. X  {
  1031. X    long size;
  1032. X    char *buffer;
  1033. X  };
  1034. X
  1035. X/* Initialize a linebuffer for use */
  1036. X
  1037. Xvoid
  1038. Xinitbuffer (linebuffer)
  1039. X     struct linebuffer *linebuffer;
  1040. X{
  1041. X  linebuffer->size = 200;
  1042. X  linebuffer->buffer = (char *) xmalloc (200);
  1043. X}
  1044. X
  1045. X/* Read a line of text from `stream' into `linebuffer'.
  1046. X Return the length of the line.  */
  1047. X
  1048. Xlong
  1049. Xreadline (linebuffer, stream)
  1050. X     struct linebuffer *linebuffer;
  1051. X     FILE *stream;
  1052. X{
  1053. X  char *buffer = linebuffer->buffer;
  1054. X  char *p = linebuffer->buffer;
  1055. X  char *end = p + linebuffer->size;
  1056. X
  1057. X  while (1)
  1058. X    {
  1059. X      int c = getc (stream);
  1060. X      if (p == end)
  1061. X    {
  1062. X      buffer = (char *) xrealloc (buffer, linebuffer->size *= 2);
  1063. X      p += buffer - linebuffer->buffer;
  1064. X      end += buffer - linebuffer->buffer;
  1065. X      linebuffer->buffer = buffer;
  1066. X    }
  1067. X      if (c < 0 || c == '\n')
  1068. X    {
  1069. X      *p = 0;
  1070. X      break;
  1071. X    }
  1072. X      *p++ = c;
  1073. X    }
  1074. X
  1075. X  return p - buffer;
  1076. X}
  1077. X
  1078. X/* Sort an input file too big to sort in core.  */
  1079. X
  1080. Xvoid
  1081. Xsort_offline (infile, nfiles, total, outfile)
  1082. X     char *infile;
  1083. X     long total;
  1084. X     char *outfile;
  1085. X{
  1086. X  int ntemps = 2 * (total + MAX_IN_CORE_SORT - 1) / MAX_IN_CORE_SORT;  /* More than enough */
  1087. X  char **tempfiles = (char **) xmalloc (ntemps * sizeof (char *));
  1088. X  FILE *istream = fopen (infile, "r");
  1089. X  int i;
  1090. X  struct linebuffer lb;
  1091. X  long linelength;
  1092. X  int failure = 0;
  1093. X
  1094. X  initbuffer (&lb);
  1095. X
  1096. X  /* Read in one line of input data.  */
  1097. X
  1098. X  linelength = readline (&lb, istream);
  1099. X
  1100. X  if (lb.buffer[0] != '\\')
  1101. X    {
  1102. X      error ("%s: not a texinfo index file", infile);
  1103. X      return;
  1104. X    }
  1105. X
  1106. X  /* Split up the input into `ntemps' temporary files, or maybe fewer,
  1107. X    and put the new files' names into `tempfiles' */
  1108. X
  1109. X  for (i = 0; i < ntemps; i++)
  1110. X    {
  1111. X      char *outname = maketempname (++tempcount);
  1112. X      FILE *ostream = fopen (outname, "w");
  1113. X      long tempsize = 0;
  1114. X
  1115. X      if (!ostream) pfatal_with_name (outname);
  1116. X      tempfiles[i] = outname;
  1117. X
  1118. X      /* Copy lines into this temp file as long as it does not make file "too big"
  1119. X    or until there are no more lines.  */
  1120. X
  1121. X      while (tempsize + linelength + 1 <= MAX_IN_CORE_SORT)
  1122. X    {
  1123. X      tempsize += linelength + 1;
  1124. X      fputs (lb.buffer, ostream);
  1125. X      putc ('\n', ostream);
  1126. X
  1127. X      /* Read another line of input data.  */
  1128. X
  1129. X      linelength = readline (&lb, istream);
  1130. X      if (!linelength && feof (istream)) break;
  1131. X
  1132. X      if (lb.buffer[0] != '\\')
  1133. X        {
  1134. X          error ("%s: not a texinfo index file", infile);
  1135. X          failure = 1;
  1136. X          goto fail;
  1137. X        }
  1138. X    }
  1139. X      fclose (ostream);
  1140. X      if (feof (istream)) break;
  1141. X    }
  1142. X
  1143. X  free (lb.buffer);
  1144. X
  1145. X fail:
  1146. X  /* Record number of temp files we actually needed.  */
  1147. X
  1148. X  ntemps = i;
  1149. X
  1150. X  /* Sort each tempfile into another tempfile.
  1151. X    Delete the first set of tempfiles and put the names of the second into `tempfiles' */
  1152. X
  1153. X  for (i = 0; i < ntemps; i++)
  1154. X    {
  1155. X      char *newtemp = maketempname (++tempcount);
  1156. X      sort_in_core (&tempfiles[i], MAX_IN_CORE_SORT, newtemp);
  1157. X      if (!keep_tempfiles)
  1158. X        unlink (tempfiles[i]);
  1159. X      tempfiles[i] = newtemp;
  1160. X    }
  1161. X
  1162. X  if (failure)
  1163. X    return;
  1164. X
  1165. X  /* Merge the tempfiles together and indexify */
  1166. X
  1167. X  merge_files (tempfiles, ntemps, outfile);
  1168. X}
  1169. X
  1170. X/* Sort `infile', whose size is `total',
  1171. X assuming that is small enough to be done in-core,
  1172. X then indexify it and send the output to `outfile' (or to stdout).  */
  1173. X
  1174. Xvoid
  1175. Xsort_in_core (infile, total, outfile)
  1176. X     char *infile;
  1177. X     long total;
  1178. X     char *outfile;
  1179. X{
  1180. X  char **nextline;
  1181. X  char *data = (char *) xmalloc (total + 1);
  1182. X  char *file_data;
  1183. X  long file_size;
  1184. X  int i;
  1185. X  FILE *ostream = stdout;
  1186. X  struct lineinfo *lineinfo;
  1187. X
  1188. X  /* Read the contents of the file into the moby array `data' */
  1189. X
  1190. X  int desc = open (infile, 0, 0);
  1191. X
  1192. X  if (desc < 0)
  1193. X    fatal ("failure reopening %s", infile);
  1194. X  for (file_size = 0; ; )
  1195. X    {
  1196. X      if ((i = read (desc, data + file_size, total - file_size)) <= 0)
  1197. X    break;
  1198. X      file_size += i;
  1199. X    }
  1200. X  file_data = data;
  1201. X  data[file_size] = 0;
  1202. X
  1203. X  close (desc);
  1204. X
  1205. X  if (file_size > 0 && data[0] != '\\')
  1206. X    {
  1207. X      error ("%s: not a texinfo index file", infile);
  1208. X      return;
  1209. X    }
  1210. X
  1211. X  init_char_order ();
  1212. X
  1213. X  /* Sort routines want to know this address */
  1214. X
  1215. X  text_base = data;
  1216. X
  1217. X  /* Create the array of pointers to lines, with a default size frequently enough.  */
  1218. X
  1219. X  lines = total / 50;
  1220. X  if (!lines) lines = 2;
  1221. X  linearray = (char **) xmalloc (lines * sizeof (char *));
  1222. X
  1223. X  /* `nextline' points to the next free slot in this array.
  1224. X     `lines' is the allocated size.  */
  1225. X
  1226. X  nextline = linearray;
  1227. X
  1228. X  /* Parse the input file's data, and make entries for the lines.  */
  1229. X
  1230. X  nextline = parsefile (infile, nextline, file_data, file_size);
  1231. X  if (nextline == 0)
  1232. X    {
  1233. X      error ("%s: not a texinfo index file", infile);
  1234. X      return;
  1235. X    }
  1236. X
  1237. X  /* Sort the lines */
  1238. X
  1239. X  /* If we have enough space, find the first keyfield of each line in advance.
  1240. X    Make a `struct lineinfo' for each line, which records the keyfield
  1241. X    as well as the line, and sort them.  */
  1242. X
  1243. X  lineinfo = (struct lineinfo *) malloc ((nextline - linearray) * sizeof (struct lineinfo));
  1244. X
  1245. X  if (lineinfo)
  1246. X    {
  1247. X      struct lineinfo *lp;
  1248. X      char **p;
  1249. X
  1250. X      for (lp = lineinfo, p = linearray; p != nextline; lp++, p++)
  1251. X    {
  1252. X      lp->text = *p;
  1253. X      lp->key.text = find_field (keyfields, *p, &lp->keylen);
  1254. X      if (keyfields->numeric)
  1255. X        lp->key.number = find_value (lp->key.text, lp->keylen);
  1256. X    }
  1257. X
  1258. X      qsort (lineinfo, nextline - linearray, sizeof (struct lineinfo), compare_prepared);
  1259. X
  1260. X      for (lp = lineinfo, p = linearray; p != nextline; lp++, p++)
  1261. X    *p = lp->text;
  1262. X
  1263. X      free (lineinfo);
  1264. X    }
  1265. X  else
  1266. X    qsort (linearray, nextline - linearray, sizeof (char *), compare_full);
  1267. X
  1268. X  /* Open the output file */
  1269. X
  1270. X  if (outfile)
  1271. X    {
  1272. X      ostream = fopen (outfile, "w");
  1273. X      if (!ostream)
  1274. X    pfatal_with_name (outfile);
  1275. X    }
  1276. X
  1277. X  writelines (linearray, nextline - linearray, ostream);
  1278. X  if (outfile) fclose (ostream);
  1279. X
  1280. X  free (linearray);
  1281. X  free (data);
  1282. X}
  1283. X
  1284. X/* Parse an input string in core into lines.
  1285. X   DATA is the input string, and SIZE is its length.
  1286. X   Data goes in LINEARRAY starting at NEXTLINE.
  1287. X   The value returned is the first entry in LINEARRAY still unused.
  1288. X   Value 0 means input file contents are invalid.  */
  1289. X
  1290. Xchar **
  1291. Xparsefile (filename, nextline, data, size)
  1292. X     char *filename;
  1293. X     char **nextline;
  1294. X     char *data;
  1295. X     long size;
  1296. X{
  1297. X  char *p, *end;
  1298. X  char **line = nextline;
  1299. X
  1300. X  p = data;
  1301. X  end = p + size;
  1302. X  *end = 0;
  1303. X
  1304. X  while (p != end)
  1305. X    {
  1306. X      if (p[0] != '\\')
  1307. X    return 0;
  1308. X
  1309. X      *line = p;
  1310. X      while (*p && *p != '\n') p++;
  1311. X      if (p != end) p++;
  1312. X
  1313. X      line++;
  1314. X      if (line == linearray + lines)
  1315. X    {
  1316. X      char **old = linearray;
  1317. X      linearray = (char **) xrealloc (linearray, sizeof (char *) * (lines *= 4));
  1318. X      line += linearray - old;
  1319. X    }
  1320. X    }
  1321. X
  1322. X  return line;
  1323. X}
  1324. X
  1325. X/* Indexification is a filter applied to the sorted lines
  1326. X as they are being written to the output file.
  1327. X Multiple entries for the same name, with different page numbers,
  1328. X get combined into a single entry with multiple page numbers.
  1329. X The first braced field, which is used for sorting, is discarded.
  1330. X However, its first character is examined, folded to lower case,
  1331. X and if it is different from that in the previous line fed to us
  1332. X a \initial line is written with one argument, the new initial.
  1333. X
  1334. X If an entry has four braced fields, then the second and third
  1335. X constitute primary and secondary names.
  1336. X In this case, each change of primary name
  1337. X generates a \primary line which contains only the primary name,
  1338. X and in between these are \secondary lines which contain
  1339. X just a secondary name and page numbers.
  1340. X*/
  1341. X
  1342. X/* The last primary name we wrote a \primary entry for.
  1343. X If only one level of indexing is being done, this is the last name seen */
  1344. Xchar *lastprimary;
  1345. Xint lastprimarylength;  /* Length of storage allocated for lastprimary */
  1346. X
  1347. X/* Similar, for the secondary name. */
  1348. Xchar *lastsecondary;
  1349. Xint lastsecondarylength;
  1350. X
  1351. X/* Zero if we are not in the middle of writing an entry.
  1352. X One if we have written the beginning of an entry but have not
  1353. X  yet written any page numbers into it.
  1354. X Greater than one if we have written the beginning of an entry
  1355. X  plus at least one page number. */
  1356. Xint pending;
  1357. X
  1358. X/* The initial (for sorting purposes) of the last primary entry written.
  1359. X When this changes, a \initial {c} line is written */
  1360. X
  1361. Xchar * lastinitial;
  1362. X
  1363. Xint lastinitiallength;
  1364. X
  1365. X/* When we need a string of length 1 for the value of lastinitial,
  1366. X   store it here.  */
  1367. X
  1368. Xchar lastinitial1[2];
  1369. X
  1370. X/* Initialize static storage for writing an index */
  1371. X
  1372. Xvoid
  1373. Xinit_index ()
  1374. X{
  1375. X  pending = 0;
  1376. X  lastinitial = lastinitial1;
  1377. X  lastinitial1[0] = 0;
  1378. X  lastinitial1[1] = 0;
  1379. X  lastinitiallength = 0;
  1380. X  lastprimarylength = 100;
  1381. X  lastprimary = (char *) xmalloc (lastprimarylength + 1);
  1382. X  bzero (lastprimary, lastprimarylength + 1);
  1383. X  lastsecondarylength = 100;
  1384. X  lastsecondary = (char *) xmalloc (lastsecondarylength + 1);
  1385. X  bzero (lastsecondary, lastsecondarylength + 1);
  1386. X}
  1387. X
  1388. X/* Indexify.  Merge entries for the same name,
  1389. X insert headers for each initial character, etc.  */
  1390. X
  1391. Xindexify (line, ostream)
  1392. X     char *line;
  1393. X     FILE *ostream;
  1394. X{
  1395. X  char *primary, *secondary, *pagenumber;
  1396. X  int primarylength, secondarylength, pagelength;
  1397. X  int len = strlen (line);
  1398. X  int nosecondary;
  1399. X  int initiallength;
  1400. X  char *initial;
  1401. X  char initial1[2];
  1402. X  register char *p;
  1403. X
  1404. X  /* First, analyze the parts of the entry fed to us this time */
  1405. X
  1406. X  p = find_braced_pos (line, 0, 0, 0);
  1407. X  if (*p == '{')
  1408. X    {
  1409. X      initial = p;
  1410. X      /* Get length of inner pair of braces starting at p,
  1411. X     including that inner pair of braces.  */
  1412. X      initiallength = find_braced_end (p + 1) + 1 - p;
  1413. X    }
  1414. X  else
  1415. X    {
  1416. X      initial = initial1;
  1417. X      initial1[0] = *p;
  1418. X      initial1[1] = 0;
  1419. X      initiallength = 1;
  1420. X
  1421. X      if (initial1[0] >= 'a' && initial1[0] <= 'z')
  1422. X    initial1[0] -= 040;
  1423. X    }
  1424. X
  1425. X  pagenumber = find_braced_pos (line, 1, 0, 0);
  1426. X  pagelength = find_braced_end (pagenumber) - pagenumber;
  1427. X  if (pagelength == 0)
  1428. X    abort ();
  1429. X
  1430. X  primary = find_braced_pos (line, 2, 0, 0);
  1431. X  primarylength = find_braced_end (primary) - primary;
  1432. X
  1433. X  secondary = find_braced_pos (line, 3, 0, 0);
  1434. X  nosecondary = !*secondary;
  1435. X  if (!nosecondary)
  1436. X    secondarylength = find_braced_end (secondary) - secondary;
  1437. X
  1438. X  /* If the primary is different from before, make a new primary entry */
  1439. X  if (strncmp (primary, lastprimary, primarylength))
  1440. X    {
  1441. X      /* Close off current secondary entry first, if one is open */
  1442. X      if (pending)
  1443. X    {
  1444. X      fputs ("}\n", ostream);
  1445. X      pending = 0;
  1446. X    }
  1447. X
  1448. X      /* If this primary has a different initial, include an entry for the initial */
  1449. X      if (initiallength != lastinitiallength ||
  1450. X      strncmp (initial, lastinitial, initiallength))
  1451. X    {
  1452. X      fprintf (ostream, "\\initial {");
  1453. X      fwrite (initial, 1, initiallength, ostream);
  1454. X      fprintf (ostream, "}\n", initial);
  1455. X      if (initial == initial1)
  1456. X        {
  1457. X          lastinitial = lastinitial1;
  1458. X          *lastinitial1 = *initial1;
  1459. X        }
  1460. X      else
  1461. X        {
  1462. X          lastinitial = initial;
  1463. X        }
  1464. X      lastinitiallength = initiallength;
  1465. X    }
  1466. X
  1467. X      /* Make the entry for the primary.  */
  1468. X      if (nosecondary)
  1469. X    fputs ("\\entry {", ostream);
  1470. X      else
  1471. X    fputs ("\\primary {", ostream);
  1472. X      fwrite (primary, primarylength, 1, ostream);
  1473. X      if (nosecondary)
  1474. X    {
  1475. X      fputs ("}{", ostream);
  1476. X      pending = 1;
  1477. X    }
  1478. X      else
  1479. X    fputs ("}\n", ostream);
  1480. X
  1481. X      /* Record name of most recent primary */
  1482. X      if (lastprimarylength < primarylength)
  1483. X    {
  1484. X          lastprimarylength = primarylength + 100;
  1485. X      lastprimary = (char *) xrealloc (lastprimary,
  1486. X                       1 + lastprimarylength);
  1487. X    }
  1488. X      strncpy (lastprimary, primary, primarylength);
  1489. X      lastprimary[primarylength] = 0;
  1490. X
  1491. X      /* There is no current secondary within this primary, now */
  1492. X      lastsecondary[0] = 0;
  1493. X    }
  1494. X
  1495. X  /* Should not have an entry with no subtopic following one with a subtopic */
  1496. X
  1497. X  if (nosecondary && *lastsecondary)
  1498. X    error ("entry %s follows an entry with a secondary name", line);
  1499. X
  1500. X  /* Start a new secondary entry if necessary */
  1501. X  if (!nosecondary && strncmp (secondary, lastsecondary, secondarylength))
  1502. X    {
  1503. X      if (pending)
  1504. X    {
  1505. X      fputs ("}\n", ostream);
  1506. X      pending = 0;
  1507. X    }
  1508. X
  1509. X      /* Write the entry for the secondary.  */
  1510. X      fputs ("\\secondary {", ostream);
  1511. X      fwrite (secondary, secondarylength, 1, ostream);
  1512. X      fputs ("}{", ostream);
  1513. X      pending = 1;
  1514. X
  1515. X      /* Record name of most recent secondary */
  1516. X      if (lastsecondarylength < secondarylength)
  1517. X    {
  1518. X          lastsecondarylength = secondarylength + 100;
  1519. X      lastsecondary = (char *) xrealloc (lastsecondary,
  1520. X                       1 + lastsecondarylength);
  1521. X    }
  1522. X      strncpy (lastsecondary, secondary, secondarylength);
  1523. X      lastsecondary[secondarylength] = 0;
  1524. X    }
  1525. X
  1526. X  /* Here to add one more page number to the current entry */
  1527. X  if (pending++ != 1)
  1528. X    fputs (", ", ostream);    /* Punctuate first, if this is not the first */
  1529. X  fwrite (pagenumber, pagelength, 1, ostream);
  1530. X}
  1531. X
  1532. X/* Close out any unfinished output entry */
  1533. X
  1534. Xvoid
  1535. Xfinish_index (ostream)
  1536. X     FILE *ostream;
  1537. X{
  1538. X  if (pending)
  1539. X    fputs ("}\n", ostream);
  1540. X  free (lastprimary);
  1541. X  free (lastsecondary);
  1542. X}
  1543. X
  1544. X/* Copy the lines in the sorted order.
  1545. X Each line is copied out of the input file it was found in. */
  1546. X
  1547. Xvoid
  1548. Xwritelines (linearray, nlines, ostream)
  1549. X     char **linearray;
  1550. X     int nlines;
  1551. X     FILE *ostream;
  1552. X{
  1553. X  char **stop_line = linearray + nlines;
  1554. X  char **next_line;
  1555. X
  1556. X  init_index ();
  1557. X
  1558. X  /* Output the text of the lines, and free the buffer space */
  1559. X
  1560. X  for (next_line = linearray; next_line != stop_line; next_line++)
  1561. X    {
  1562. X      /* If -u was specified, output the line only if distinct from previous one.  */
  1563. X      if (next_line == linearray
  1564. X      /* Compare previous line with this one, using only the explicitly specd keyfields */
  1565. X      || compare_general (*(next_line - 1), *next_line, 0L, 0L, num_keyfields - 1))
  1566. X    {
  1567. X      char *p = *next_line;
  1568. X      char c;
  1569. X      while ((c = *p++) && c != '\n');
  1570. X      *(p-1) = 0;
  1571. X      indexify (*next_line, ostream);
  1572. X    }
  1573. X    }
  1574. X
  1575. X  finish_index (ostream);
  1576. X}
  1577. X
  1578. X/* Assume (and optionally verify) that each input file is sorted;
  1579. X merge them and output the result.
  1580. X Returns nonzero if any input file fails to be sorted.
  1581. X
  1582. X This is the high-level interface that can handle an unlimited number of files.  */
  1583. X
  1584. X#define MAX_DIRECT_MERGE 10
  1585. X
  1586. Xint
  1587. Xmerge_files (infiles, nfiles, outfile)
  1588. X     char **infiles;
  1589. X     int nfiles;
  1590. X     char *outfile;
  1591. X{
  1592. X  char **tempfiles;
  1593. X  int ntemps;
  1594. X  int i;
  1595. X  int value = 0;
  1596. X  int start_tempcount = tempcount;
  1597. X
  1598. X  if (nfiles <= MAX_DIRECT_MERGE)
  1599. X    return merge_direct (infiles, nfiles, outfile);
  1600. X
  1601. X  /* Merge groups of MAX_DIRECT_MERGE input files at a time,
  1602. X     making a temporary file to hold each group's result.  */
  1603. X
  1604. X  ntemps = (nfiles + MAX_DIRECT_MERGE - 1) / MAX_DIRECT_MERGE;
  1605. X  tempfiles = (char **) xmalloc (ntemps * sizeof (char *));
  1606. X  for (i = 0; i < ntemps; i++)
  1607. X    {
  1608. X      int nf = MAX_DIRECT_MERGE;
  1609. X      if (i + 1 == ntemps)
  1610. X    nf = nfiles - i * MAX_DIRECT_MERGE;
  1611. X      tempfiles[i] = maketempname (++tempcount);
  1612. X      value |= merge_direct (&infiles[i * MAX_DIRECT_MERGE], nf, tempfiles[i]);
  1613. X    }
  1614. X
  1615. X  /* All temporary files that existed before are no longer needed
  1616. X     since their contents have been merged into our new tempfiles.
  1617. X     So delete them.  */
  1618. X  flush_tempfiles (start_tempcount);
  1619. X
  1620. X  /* Now merge the temporary files we created.  */
  1621. X
  1622. X  merge_files (tempfiles, ntemps, outfile);
  1623. X
  1624. X  free (tempfiles);
  1625. X
  1626. X  return value;
  1627. X}  
  1628. X
  1629. X/* Assume (and optionally verify) that each input file is sorted;
  1630. X merge them and output the result.
  1631. X Returns nonzero if any input file fails to be sorted.
  1632. X
  1633. X This version of merging will not work if the number of
  1634. X input files gets too high.  Higher level functions
  1635. X use it only with a bounded number of input files.  */
  1636. X
  1637. Xint
  1638. Xmerge_direct (infiles, nfiles, outfile)
  1639. X     char **infiles;
  1640. X     int nfiles;
  1641. X     char *outfile;
  1642. X{
  1643. X  char **ip = infiles;
  1644. X  struct linebuffer *lb1, *lb2;
  1645. X  struct linebuffer **thisline, **prevline;
  1646. X  FILE **streams;
  1647. X  int i;
  1648. X  int nleft;
  1649. X  int lossage = 0;
  1650. X  int *file_lossage;
  1651. X  struct linebuffer *prev_out = 0;
  1652. X  FILE *ostream = stdout;
  1653. X
  1654. X  if (outfile)
  1655. X    {
  1656. X      ostream = fopen (outfile, "w");
  1657. X    }
  1658. X  if (!ostream) pfatal_with_name (outfile);
  1659. X
  1660. X  init_index ();
  1661. X
  1662. X  if (nfiles == 0)
  1663. X    {
  1664. X      if (outfile)
  1665. X        fclose (ostream);
  1666. X      return 0;
  1667. X    }
  1668. X
  1669. X  /* For each file, make two line buffers.
  1670. X     Also, for each file, there is an element of `thisline'
  1671. X     which points at any time to one of the file's two buffers,
  1672. X     and an element of `prevline' which points to the other buffer.
  1673. X     `thisline' is supposed to point to the next available line from the file,
  1674. X     while `prevline' holds the last file line used,
  1675. X     which is remembered so that we can verify that the file is properly sorted. */
  1676. X
  1677. X  /* lb1 and lb2 contain one buffer each per file */
  1678. X  lb1 = (struct linebuffer *) xmalloc (nfiles * sizeof (struct linebuffer));
  1679. X  lb2 = (struct linebuffer *) xmalloc (nfiles * sizeof (struct linebuffer));
  1680. X
  1681. X  /* thisline[i] points to the linebuffer holding the next available line in file i,
  1682. X     or is zero if there are no lines left in that file.  */
  1683. X  thisline = (struct linebuffer **) xmalloc (nfiles * sizeof (struct linebuffer *));
  1684. X  /* prevline[i] points to the linebuffer holding the last used line from file i.
  1685. X     This is just for verifying that file i is properly sorted.  */
  1686. X  prevline = (struct linebuffer **) xmalloc (nfiles * sizeof (struct linebuffer *));
  1687. X  /* streams[i] holds the input stream for file i.  */
  1688. X  streams = (FILE **) xmalloc (nfiles * sizeof (FILE *));
  1689. X  /* file_lossage[i] is nonzero if we already know file i is not properly sorted.  */
  1690. X  file_lossage = (int *) xmalloc (nfiles * sizeof (int));
  1691. X
  1692. X  /* Allocate and initialize all that storage */
  1693. X
  1694. X  for (i = 0; i < nfiles; i++)
  1695. X    {
  1696. X      initbuffer (&lb1[i]);
  1697. X      initbuffer (&lb2[i]);
  1698. X      thisline[i] = &lb1[i];
  1699. X      prevline[i] = &lb2[i];
  1700. X      file_lossage[i] = 0;
  1701. X      streams[i] = fopen (infiles[i], "r");
  1702. X      if (!streams[i])
  1703. X    pfatal_with_name (infiles[i]);
  1704. X
  1705. X      readline (thisline[i], streams[i]);
  1706. X    }
  1707. X
  1708. X  /* Keep count of number of files not at eof */
  1709. X  nleft = nfiles;
  1710. X
  1711. X  while (nleft)
  1712. X    {
  1713. X      struct linebuffer *best = 0;
  1714. X      struct linebuffer *exch;
  1715. X      int bestfile = -1;
  1716. X      int i;
  1717. X
  1718. X      /* Look at the next avail line of each file; choose the least one.  */
  1719. X
  1720. X      for (i = 0; i < nfiles; i++)
  1721. X    {
  1722. X      if (thisline[i] &&
  1723. X          (!best ||
  1724. X           0 < compare_general (best->buffer, thisline[i]->buffer,
  1725. X                    (long) bestfile, (long) i, num_keyfields)))
  1726. X        {
  1727. X          best = thisline[i];
  1728. X          bestfile = i;
  1729. X        }
  1730. X    }
  1731. X
  1732. X      /* Output that line, unless it matches the previous one and we don't want duplicates */
  1733. X
  1734. X      if (!(prev_out &&
  1735. X        !compare_general (prev_out->buffer, best->buffer, 0L, 1L, num_keyfields - 1)))
  1736. X    indexify (best->buffer, ostream);
  1737. X      prev_out = best;
  1738. X
  1739. X      /* Now make the line the previous of its file, and fetch a new line from that file */
  1740. X
  1741. X      exch = prevline[bestfile];
  1742. X      prevline[bestfile] = thisline[bestfile];
  1743. X      thisline[bestfile] = exch;
  1744. X
  1745. X      while (1)
  1746. X    {
  1747. X      /* If the file has no more, mark it empty */
  1748. X
  1749. X      if (feof (streams[bestfile]))
  1750. X        {
  1751. X          thisline[bestfile] = 0;
  1752. X          nleft--;        /* Update the number of files still not empty */
  1753. X          break;
  1754. X        }
  1755. X      readline (thisline[bestfile], streams[bestfile]);
  1756. X      if (thisline[bestfile]->buffer[0] || !feof (streams[bestfile])) break;
  1757. X    }
  1758. X    }
  1759. X
  1760. X  finish_index (ostream);
  1761. X
  1762. X  /* Free all storage and close all input streams */
  1763. X
  1764. X  for (i = 0; i < nfiles; i++)
  1765. X    {
  1766. X      fclose (streams[i]);
  1767. X      free (lb1[i].buffer);
  1768. X      free (lb2[i].buffer);
  1769. X    }
  1770. X  free (file_lossage);
  1771. X  free (lb1);
  1772. X  free (lb2);
  1773. X  free (thisline);
  1774. X  free (prevline);
  1775. X  free (streams);
  1776. X
  1777. X  if (outfile)
  1778. X    fclose (ostream);
  1779. X
  1780. X  return lossage;
  1781. X}
  1782. X
  1783. X/* Print error message and exit.  */
  1784. X
  1785. Xfatal (s1, s2)
  1786. X     char *s1, *s2;
  1787. X{
  1788. X  error (s1, s2);
  1789. X  exit (EXIT_FATAL);
  1790. X}
  1791. X
  1792. X/* Print error message.  `s1' is printf control string, `s2' is arg for it. */
  1793. X
  1794. Xerror (s1, s2)
  1795. X     char *s1, *s2;
  1796. X{
  1797. X  printf ("texindex: ");
  1798. X  printf (s1, s2);
  1799. X  printf ("\n");
  1800. X}
  1801. X
  1802. Xperror_with_name (name)
  1803. X     char *name;
  1804. X{
  1805. X#ifdef VMS
  1806. X#include <errno.h>
  1807. X  extern noshare int sys_nerr;
  1808. X  extern noshare char *sys_errlist[];
  1809. X#else
  1810. X  extern int errno, sys_nerr;
  1811. X  extern char *sys_errlist[];
  1812. X#endif
  1813. X  char *s;
  1814. X
  1815. X  if (errno < sys_nerr)
  1816. X    s = concat ("", sys_errlist[errno], " for %s");
  1817. X  else
  1818. X    s = "cannot open %s";
  1819. X  error (s, name);
  1820. X}
  1821. X
  1822. Xpfatal_with_name (name)
  1823. X     char *name;
  1824. X{
  1825. X  extern int errno, sys_nerr;
  1826. X  extern char *sys_errlist[];
  1827. X  char *s;
  1828. X
  1829. X  if (errno < sys_nerr)
  1830. X    s = concat ("", sys_errlist[errno], " for %s");
  1831. X  else
  1832. X    s = "cannot open %s";
  1833. X  fatal (s, name);
  1834. X}
  1835. X
  1836. X/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1837. X
  1838. Xchar *
  1839. Xconcat (s1, s2, s3)
  1840. X     char *s1, *s2, *s3;
  1841. X{
  1842. X  int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1843. X  char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1844. X
  1845. X  strcpy (result, s1);
  1846. X  strcpy (result + len1, s2);
  1847. X  strcpy (result + len1 + len2, s3);
  1848. X  *(result + len1 + len2 + len3) = 0;
  1849. X
  1850. X  return result;
  1851. X}
  1852. X
  1853. X/* Like malloc but get fatal error if memory is exhausted.  */
  1854. X
  1855. Xint
  1856. Xxmalloc (size)
  1857. X     int size;
  1858. X{
  1859. X  int result = malloc (size);
  1860. X  if (!result)
  1861. X    fatal ("virtual memory exhausted", 0);
  1862. X  return result;
  1863. X}
  1864. X
  1865. X
  1866. Xint
  1867. Xxrealloc (ptr, size)
  1868. X     char *ptr;
  1869. X     int size;
  1870. X{
  1871. X  int result = realloc (ptr, size);
  1872. X  if (!result)
  1873. X    fatal ("virtual memory exhausted");
  1874. X  return result;
  1875. X}
  1876. X
  1877. Xbzero (b, length)
  1878. X     register char *b;
  1879. X     register int length;
  1880. X{
  1881. X#ifdef VMS
  1882. X  short zero = 0;
  1883. X  long max_str = 65535;
  1884. X
  1885. X  while (length > max_str) {
  1886. X    (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  1887. X    length -= max_str;
  1888. X    b += max_str;
  1889. X  }
  1890. X  (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  1891. X#else
  1892. X  while (length-- > 0)
  1893. X    *b++ = 0;
  1894. X#endif /* not VMS */
  1895. X}
  1896. END_OF_FILE
  1897.   if test 42774 -ne `wc -c <'./support/texindex.c'`; then
  1898.     echo shar: \"'./support/texindex.c'\" unpacked with wrong size!
  1899.   fi
  1900.   # end of './support/texindex.c'
  1901. fi
  1902. echo shar: End of archive 1 \(of 16\).
  1903. cp /dev/null ark1isdone
  1904. MISSING=""
  1905. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ; do
  1906.     if test ! -f ark${I}isdone ; then
  1907.     MISSING="${MISSING} ${I}"
  1908.     fi
  1909. done
  1910. if test "${MISSING}" = "" ; then
  1911.     echo You have unpacked all 16 archives.
  1912.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1913. else
  1914.     echo You still must unpack the following archives:
  1915.     echo "        " ${MISSING}
  1916. fi
  1917. exit 0
  1918. exit 0 # Just in case...
  1919.